package lastRemaining;

import java.util.ArrayList;
import java.util.List;

public class Solution {
    public int lastRemaining(int n, int m) {
     List<Integer> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(i);
        }
        return f(list,0,m);
    }
    private int f(List<Integer> rest,int start,int m) {
        if(rest.size() == 1){
            return rest.get(0);
        }
        // 删除第m个元素
        int deletIndex = (start+m)%rest.size();
        rest.remove(deletIndex);
        return f(rest,deletIndex,m);
    }

}
